from PyQt4 import QtGui, QtCore
import os, random, gTile, gSystem
from gJson import loadString, loadInteger, loadObject, loadArray, loadCoordinates

class City(object):
    
    def __init__(self, nam, loc, spr, pop):
        self._name = nam
        self._loc = loc
        self._sprite = spr
        self._population = pop
        self._buffs = []
        self.ID = None

        self.createTile()

    def name(self):
        return self._name

    def population(self):
        return self._population

    def sprite(self):
        return self._sprite

    def getLoc(self):
        return self._loc

    def setID(self, newid):
        self.ID = newid

    def createTile(self):
        self.tile = gSystem.mainWindow.glwidget.createImage(os.path.join("data", "images", "units", self._sprite), gSystem.CITY_LAYER, 
                                                    (0, 0, 31, 31), 
                                                    (self._loc[0]*32, self._loc[1]*32, 32, 32))

    def dump(self):
        return dict(name=self._name, loc=self._loc, sprite=self._sprite, population=self._population)
    
    @staticmethod
    def load(obj, sess):
        city = City(
            loadString('City.name', obj.get('name')),
            loadCoordinates('City.loc', obj.get('loc')),
            loadString('City.sprite', obj.get('sprite')),
            loadInteger('City.population', obj.get('population')))
        return city
